home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / hMailServer / hMailServer-4.1-Build-136.exe / {app} / Addons / Squirrelmail Plugins / hmailserver_changepass / options.php < prev    next >
PHP Script  |  2005-03-14  |  11KB  |  358 lines

  1. <?php
  2.  
  3.    /*
  4.     *  Change hMailServer MySQL Password plugin 1.0
  5.     *
  6.     */
  7.  
  8.    chdir ('..');
  9.    if (!defined('SM_PATH')) define('SM_PATH','../');
  10.  
  11.         
  12.  
  13.    // include compatibility plugin
  14.    //
  15.    if (file_exists('./plugins/compatibility/functions.php'))
  16.       include_once('./plugins/compatibility/functions.php');
  17.    else if (file_exists('../plugins/compatibility/functions.php'))
  18.       include_once('../plugins/compatibility/functions.php');
  19.  
  20.  
  21.  
  22.  
  23.    // get global variables for versions of PHP < 4.1
  24.    //
  25.    if (!compatibility_check_php_version(4, 1)) {
  26.       global $HTTP_SERVER_VARS, $HTTP_POST_VARS;
  27.       $_SERVER = $HTTP_SERVER_VARS;
  28.       $_POST = $HTTP_POST_VARS;
  29.    }
  30.  
  31.  
  32.    global $color;
  33.    global $split_username_query, $split_username_expression, 
  34.           $split_username_domain_field, $debug;
  35.  
  36.  
  37.    $isForceChange = stristr($_SERVER['PHP_SELF'], 'right_main.php')
  38.                  || stristr($_SERVER['PHP_SELF'], 'webmail.php');
  39.  
  40.  
  41.    if (!$isForceChange)
  42.    {
  43.  
  44.       if (compatibility_check_sm_version(1, 3))
  45.       {
  46.          include_once (SM_PATH . 'include/validate.php');
  47.          include_once (SM_PATH . 'functions/page_header.php');
  48.          include_once (SM_PATH . 'functions/imap.php');
  49.          include_once (SM_PATH . 'include/load_prefs.php');
  50.          include_once (SM_PATH . 'functions/i18n.php');
  51.          include_once (SM_PATH . 'config/config.php');
  52.          include_once (SM_PATH . 'functions/strings.php');
  53.          include_once (SM_PATH . 'functions/imap.php');
  54.       }
  55.       else
  56.       {
  57.          include_once ('../src/validate.php');
  58.          include_once ('../functions/page_header.php');
  59.          include_once ('../functions/imap.php');
  60.          include_once ('../src/load_prefs.php');
  61.          include_once ('../functions/i18n.php');
  62.          include_once ('../config/config.php');
  63.          include_once ('../functions/strings.php');
  64.          include_once ('../functions/imap.php');
  65.       }
  66.  
  67.    }
  68.  
  69.  
  70.    // get flag that tells us if this is a password submission attempt...
  71.    //
  72.    if (isset($_POST['plugin_hmailserver_changepass']))
  73.       $plugin_hmailserver_changepass = $_POST['plugin_hmailserver_changepass'];
  74.  
  75.  
  76.    // determine URL for redirection if Cancel is pressed
  77.    // (non https if the flag is on, otherwise, use what
  78.    // was already there)
  79.    //   
  80.    $cancelLocation = 'http' . (isset($_SERVER["HTTPS"]) ? 's' : '');
  81.  
  82.    ereg("(^.*/).*", $_SERVER['REQUEST_URI'], $regs);
  83.    $cancelLocation .= '://' . $_SERVER['HTTP_HOST']
  84.       . $regs[1] . '../../src/options.php?optmode=submit&plugin_hmailserver_changepass=0';
  85.  
  86.  
  87.    // if a password change is given, check its validity
  88.    //
  89.    if (isset($plugin_hmailserver_changepass))
  90.    {
  91.        $Messages = hmailserver_changepass_check();
  92.  
  93.        // the above only returns if password wasn't changed
  94.        // so if we came from webmail.php, must redirect to
  95.        // right_main.php (need to pass messages along...)
  96.        //
  97.        if (stristr($_SERVER['PHP_SELF'], 'webmail.php'))
  98.        {
  99.           $location = $_SERVER['PHP_SELF'];
  100.           $location = str_replace('webmail.php', 'right_main.php', $location);
  101.           $location .= '?messages=';
  102.           foreach($Messages as $message) $location .= urlencode($message . '||');
  103.  
  104.           header('Location: ' . $location);
  105.           exit(0);
  106.        }
  107.    }
  108.  
  109.         
  110.    // don't display page header if we got a successful
  111.    // password change (No error messages exist and a 
  112.    // password was submitted.  Cannot send output before 
  113.    // header() call)
  114.    //
  115.    if (!isset($Messages) && isset($cp_oldpass) && $cp_oldpass)
  116.    {
  117.       // do nothing
  118.    }
  119.    else
  120.    {
  121.       // if not in force password change mode, we 
  122.       // still need to send a page header
  123.       //
  124.       if (!$isForceChange)
  125.       {
  126.          displayPageHeader($color, 'None', 'document.forms[0].elements["cp_oldpass"].focus();');
  127.          echo '<br>';
  128.       }
  129.  
  130.       echo '<table width=95% align=center cellpadding=2 cellspacing=2 border=0>';
  131.       echo '<tr><td bgcolor="';
  132.  
  133.       if (!$isForceChange)
  134.          echo $color[0] . '">';
  135.       else
  136.          echo $color[2] . '">';
  137.    
  138.    }
  139.    
  140.  
  141.    echo '<center><b>';
  142.    echo _("Change Password"); 
  143.    echo '</b></center></td>';
  144.  
  145.  
  146. if (isset($Messages) && count($Messages)) {
  147.     echo "<tr><td>\n";
  148.     foreach ($Messages as $line) {
  149.         echo htmlspecialchars($line) . "<br>\n";
  150.     }
  151.     echo "</td></tr>\n";
  152. }
  153.  
  154. ?><tr><td>
  155. <?php 
  156.  
  157.     if ($_SERVER['PHP_SELF']) {
  158.         $location=$_SERVER['PHP_SELF'];
  159.     } else {
  160.         $location="../plugins/hmailserver_changepass/options.php";
  161.     }
  162.  
  163.         $location = str_replace('right_main.php', 'webmail.php', $location);
  164.  
  165. ?>
  166.     <form method=post action="<?php echo $location; ?>">
  167.     <table>
  168.       <tr>
  169.         <th align=right><?php echo _("Old Password"); ?>:</th>
  170.         <td><input type=password name=cp_oldpass value=""  size=20></td>
  171.       </tr>
  172.       <tr>
  173.         <th align=right><?php echo _("New Password"); ?>:</th>
  174.         <td><input type=password name=cp_newpass value="" size=20></td>
  175.       </tr>
  176.       <tr>
  177.         <th align=right><?php echo _("Verify New Password"); ?>:</th>
  178.         <td><input type=password name=cp_verify value="" size=20></td>
  179.       </tr>
  180.       <tr>
  181.         <td align=right colspan=2>
  182.           <input type="hidden" name="plugin_hmailserver_changepass" value="1">
  183.           <input type=submit value="<?php echo _("Submit"); ?>">
  184. <?php 
  185.  
  186.    if (!$isForceChange)
  187.  
  188.      echo '<input type=button value="' . _("Cancel") . '" onClick="document.location=\'' . $cancelLocation . '\'"'
  189.         . ' name="plugin_hmailserver_changepass_cancel">';
  190.  
  191. ?>
  192.         </td>
  193.       </tr>
  194.     </table>
  195. </td></tr>
  196. </tr></table>
  197. </body></html>
  198. <?php
  199.  
  200. exit(0);
  201.  
  202.  
  203. function hmailserver_changepass_check() {
  204.    global $cp_oldpass, $cp_newpass, $cp_verify, $key, $onetimepad;
  205.    global $plugin_change_ldappass;
  206.  
  207.  
  208.    // get global variables for versions of PHP < 4.1
  209.    //
  210.    if (!compatibility_check_php_version(4, 1)) {
  211.       global $HTTP_SESSION_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS;
  212.       $_SESSION = $HTTP_SESSION_VARS;
  213.       $_POST = $HTTP_POST_VARS;
  214.       $_COOKIE = $HTTP_COOKIE_VARS;
  215.    }
  216.  
  217.  
  218.    $Messages = array();
  219.    $password = OneTimePadDecrypt($_COOKIE['key'], $_SESSION['onetimepad']);
  220.  
  221.    $cp_oldpass = $_POST['cp_oldpass'];
  222.    $cp_newpass = $_POST['cp_newpass'];
  223.    $cp_verify = $_POST['cp_verify'];
  224.  
  225.    if ($cp_oldpass == '')
  226.        array_push($Messages, _("You must type in your old password."));
  227.    if ($cp_newpass == '')
  228.        array_push($Messages, _("You must type in a new password."));
  229.    if ($cp_verify == '')
  230.        array_push($Messages,
  231.            _("You must also type in your new password in the verify box."));
  232.    if ($cp_newpass != '' && $cp_verify != $cp_newpass)
  233.        array_push($Messages,
  234.            _("Your new password does not match the verify password."));
  235.    if ($cp_oldpass != '' && $cp_oldpass != $password)
  236.        array_push($Messages, _("Your old password is not correct."));
  237.  
  238.    if ($cp_oldpass == $cp_newpass)
  239.     array_push($Messages, _("Your new password must be different than your old password."));
  240.  
  241.    if (count($Messages))
  242.        return $Messages;
  243.  
  244.    return hmailserver_changepass_go($password);
  245. }
  246.  
  247.  
  248. function hmailserver_changepass_go($password) {
  249.  
  250.  
  251.     global $split_username_query, $split_username_expression, 
  252.            $split_username_domain_field, $debug, $update_clear_passwd_field;
  253.  
  254.  
  255.     // get global variables for versions of PHP < 4.1
  256.     //
  257.     if (!compatibility_check_php_version(4, 1)) {
  258.        global $HTTP_SESSION_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SERVER_VARS;
  259.        $_SESSION = $HTTP_SESSION_VARS;
  260.        $_SERVER = $HTTP_SERVER_VARS;
  261.        $_POST = $HTTP_POST_VARS;
  262.        $_COOKIE = $HTTP_COOKIE_VARS;
  263.     }
  264.  
  265.     $Messages = array();
  266.  
  267.     $cp_newpass = $_POST['cp_newpass'];
  268.     $username = $_SESSION['username'];
  269.     $base_uri = $_SESSION['base_uri'];
  270.     $onetimepad = $_SESSION['onetimepad'];
  271.     $key = $_COOKIE['key'];
  272.  
  273.     $user = $username;
  274.     if ($split_username_query)
  275.     {
  276.         list($user, $domain) = preg_split('/' . $split_username_expression . '/', $username);
  277.     }
  278.  
  279.     // Extract domain part from email address
  280.     $iAtPos = strpos($username, '@');
  281.     
  282.     $DomainName = substr($username, $iAtPos + 1);
  283.  
  284.     $hCOMApp = new COM("hMailServer.Application");
  285.     
  286.     $obDomain = $hCOMApp->Domains->ItemByName($DomainName);
  287.  
  288.     if (!isset($obDomain))
  289.     {
  290.         array_push($Messages, _("Can not fetch domain object.!"));
  291.         return $Messages;        
  292.     }
  293.     
  294.     $obAccount = $obDomain->Accounts->ItemByAddress($username);
  295.     
  296.     if (!isset($obAccount))
  297.     {
  298.         array_push($Messages, _("Can not fetch account object.!"));
  299.         return $Messages;        
  300.     }
  301.     
  302.     $sOldPassword = $obAccount->Password();
  303.     
  304.     // Check that it matches ...
  305.     $sOldPasswordEntry = $_POST['cp_oldpass'];
  306.     
  307.     $hCOMUtilities = new COM("hMailServer.Utilities");
  308.  
  309.     // MD5 the entered password.
  310.     $sOldPasswordEntry = $hCOMUtilities->MD5($sOldPasswordEntry);
  311.     
  312.     if ($sOldPassword != $sOldPasswordEntry)
  313.     {
  314.         array_push($Messages, _("Strange, your old password does not match the database... rejecting."));
  315.         return $Messages;
  316.         
  317.     }
  318.     
  319.     // OK, we should change it.
  320.     
  321.     $obAccount->Password = $cp_newpass;
  322.     
  323.     // Save the change.
  324.     $obAccount->Save();
  325.  
  326.  
  327.     // Write new cookies for the password
  328.     $onetimepad = OneTimePadCreate(strlen($cp_newpass));
  329.     $_SESSION['onetimepad'] = $onetimepad;
  330.     $key = OneTimePadEncrypt($cp_newpass, $onetimepad);
  331.     setcookie('key', $key, 0, $base_uri);
  332.     
  333.     
  334.     // no longer using https:
  335.     // (unless the flag is off, in which case we don't care - just
  336.     // use what was already there)
  337.     //   
  338.     $location = 'http' . (isset($_SERVER["HTTPS"]) ? 's' : '');
  339.     
  340.     ereg("(^.*/).*", $_SERVER['REQUEST_URI'], $regs);
  341.     $location .= '://' . $_SERVER['HTTP_HOST'];
  342.     
  343.     if (strstr($_SERVER['REQUEST_URI'], 'right_main.php')
  344.     || strstr($_SERVER['REQUEST_URI'], 'webmail.php'))
  345.         $location .= $regs[1] . 'right_main.php?optmode=submit&plugin_hmailserver_changepass=1';
  346.         else
  347.         $location .= $regs[1] . '../../src/options.php?optmode=submit&plugin_hmailserver_changepass=1';
  348.     
  349.     
  350.     header("Location: $location");
  351.     exit(0);
  352.  
  353. }
  354.  
  355.  
  356.  
  357. ?>
  358.